<template>
  <el-card>
    <div slot="header" class="card-title">
      <span>{{ model.title.value }}</span>
      <div class="tools-btn" style="float: right;">
        <el-date-picker
          v-model="startTime"
          type="daterange"
          size="mini"
          clearable
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </div>
    </div>
    <div :ref="model.id.value" class="app-panel-body" :style="{height: model.height.value}">
    </div>
  </el-card>
</template>

<script>
import echarts from 'echarts'
import { getData } from '@/api/sysPay'

export default {
  name: 'PayPanel',
  props: ['model'],
  data() {
    return {
      chart: '',
      startTime: '',
      times: [],
      orderFees: [],
      orderCount: [],
      invoiceFees: [],
      invoiceCount: [],
      option: {}
    }
  },
  methods: {
    handleQuery() {
      let time = ''
      if (this.startTime) {
        time = this.startTime.join()
      }
      getData(time).then(res => {
        if (res.code === 20000) {
          this.times = res.data.times
          this.invoiceCount = res.data.invoiceCount
          this.invoiceFees = res.data.invoiceFees
          this.orderCount = res.data.orderCount
          this.orderFees = res.data.orderFees
          const chartDom = this.$refs[this.model.id.value]
          this.chart = echarts.init(chartDom)
          this.chart.setOption({
            xAxis: {
              type: 'category',
              data: this.times
            },
            yAxis: {
              type: 'value'
            },
            tooltip: {
              trigger: 'axis'
            },
            calculable: false,
            toolbox: {
              show: true,
              feature: {
                magicType: { show: true, type: ['line', 'bar'] },
                dataView: {
                  show: true
                },
                restore: {
                  show: true
                }
              }
            },
            legend: {},
            series: [
              {
                name: '订单数',
                data: this.orderCount,
                type: 'line',
                showSymbol: true,
                smooth: true
              },
              {
                name: '订单金额',
                data: this.orderFees,
                type: 'line',
                showSymbol: true,
                smooth: true
              },
              {
                name: '开票数',
                data: this.invoiceCount,
                type: 'line',
                showSymbol: true,
                smooth: true
              },
              {
                name: '开票金额',
                data: this.invoiceFees,
                type: 'line',
                showSymbol: true,
                smooth: true
              }
            ]
          })
        }
      })
    },
    onResize() {
      this.chart.resize()
    }
  },
  mounted() {
    this.handleQuery()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
  },
  watch: {
    model: {
      deep: true,
      handler(val) {
        this.onResize()
      }
    },
    startTime: {
      deep: true,
      handler(val) {
        this.handleQuery()
      }
    }
  }
}
</script>

<style scoped>

.card-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.tools-btn {
  display: flex;
  justify-content: center;
}
</style>
